//DevNETCoder

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
using System.Security.Cryptography;
using System.Text;
 
namespace STT.Business
{
    public class BTbDangNhap
    {
        public static void InsertQuyen(string tenquyen)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenQuyen", tenquyen);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "[TbQuyen_Insert]", pr);
        }
        //----------------------------//
        public static DataTable SelectbyGVHV()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_SelectByGVHV", null);
            return all;
        }
        //----------------------------//
        public static void UpdateTrangThaiTK(string MaDN, string TrangThai)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaDN", int.Parse(MaDN));
            pr[1] = new SqlParameter(@"TrangThai", int.Parse(TrangThai));
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "[TbDangNhap_UpdateTrangThaiTK]", pr);
        }
        //-----------------------------------------------------------//
        public static void Update_ChucVuDonVi(ETbGiangVien OTbGiangVien)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaGV", OTbGiangVien.MaGV);
            pr[1] = new SqlParameter(@"MaDonVi", OTbGiangVien.MaDonVi);
            pr[2] = new SqlParameter(@"MaChucVu", OTbGiangVien.MaChucVu);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDangNhap_UpdateDonViChucVu", pr);
        }
        public static void DeleteByMaQuyen(int MaQuyen)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaQuyen", MaQuyen);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDangNhap_DeleteByMaQuyen", pr);
        }
        //-------------------------------//
        public static void InsertDangNhap(ETbDangNhap DN)
        {
            SqlParameter[] pr = new SqlParameter[5];
            pr[0] = new SqlParameter(@"TenDangNhap", DN.TenDangNhap);
            pr[1] = new SqlParameter(@"MatKhau", DN.MatKhau);
            pr[2] = new SqlParameter(@"Quyen", DN.Quyen);
            pr[3] = new SqlParameter(@"RadomPass", DN.RadomPass);
            pr[4] = new SqlParameter(@"KhoaTaiKhoan", DN.KhoaTaiKhoan);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "[TbDangNhap_Insert]", pr);
        }
        //----------------------------------------//
        public static void InsertToTbGiangVien(ETbGiangVien DN)
        {
            SqlParameter[] pr = new SqlParameter[6];
            pr[0] = new SqlParameter(@"MaGV", DN.MaGV);
            pr[1] = new SqlParameter(@"TenGV", DN.TenGV);
            pr[2] = new SqlParameter(@"Sdt", DN.Sdt);
            pr[3] = new SqlParameter(@"MaDonVi", DN.MaDonVi);
            pr[4] = new SqlParameter(@"MaChucVu", DN.MaChucVu);
            pr[3] = new SqlParameter(@"MaDonVi", DN.MaDonVi);
            pr[4] = new SqlParameter(@"MaChucVu", DN.MaChucVu);
            pr[5] = new SqlParameter(@"Anh", DN.Anh);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "[TbGiangVien_InsertTKTbGiangVien]", pr);
        }

        //-----------------------------------------------------------//
        public static void UpdatePassword1(ETbDangNhap OTbDangNhap)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"TenDN", OTbDangNhap.TenDangNhap);
            pr[1] = new SqlParameter(@"passnew", OTbDangNhap.MatKhau);
            pr[2] = new SqlParameter(@"radompass", OTbDangNhap.RadomPass);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDangNhap_UpdatePassword", pr);
        }
        //----------------------------------------//
        public static void DeleteChucNangQuyen(string MaQuyen)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaQuyen", MaQuyen);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "[TbChucNang_Quyen_DeleteByMaQuyen]", pr);
        }
        //----------------------------------------//
        public static void InsertChucNangQuyen(string Maquyen, string Macn, string TrangThai)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaQuyen", Maquyen);
            pr[1] = new SqlParameter(@"MaCN", Macn);
            pr[2] = new SqlParameter(@"TrangThai", TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "[TbChucNangQuyen_Insert]", pr);
        }
        //-----------------------------------------------------------//
        public void UpdatePassword(ETbDangNhap OTbDangNhap)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"TenDN", OTbDangNhap.TenDangNhap);
            pr[1] = new SqlParameter(@"passnew", OTbDangNhap.MatKhau);
            pr[2] = new SqlParameter(@"radompass", OTbDangNhap.RadomPass);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDangNhap_UpdatePassword", pr);
        }
        //-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_GetAllGV", null);
            return all;
        }
        public static DataTable SelectAllChatForManager(string TenDN)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDN", TenDN);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_SelectAllChatForManager", pr);
            return all;
        }
        public static DataTable SelectAllChatForHocVien(string TenDN)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDN", TenDN);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_SelectAllChatForHocVien", pr);
            return all;
        }
        public static DataTable SelectAllChatForGiangVien(string TenDN)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDN", TenDN);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_SelectAllChatForGiangVien", pr);
            return all;
        }

        public static DataTable SelectByName(string Name)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Name", Name == "" ? (object)DBNull.Value : Name);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_SelectByName", pr);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_SelectTop", pr);
            return all;
        }
        //-----------------------------------------------------------//

        public static bool KiemTraDuLieu(ETbDangNhap DN)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDangNhap", DN.TenDangNhap);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static DataTable SearchDangNhap(ETbDangNhap DN)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDangNhap", DN.TenDangNhap);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_Search", pr);
            return all;
        }
        //-----------------------------------------------------------//
        public static void Update_PhanQuyen(ETbDangNhap OTbDangNhap)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaDN", OTbDangNhap.MaDN);
            pr[1] = new SqlParameter(@"Quyen", OTbDangNhap.Quyen);
            pr[2] = new SqlParameter(@"KhoaTK", OTbDangNhap.KhoaTaiKhoan);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDangNhap_Update_PhanQuyen", pr);
        }

        public string GetMD5(string str)
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] bHash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));

            StringBuilder sbHash = new StringBuilder();

            foreach (byte b in bHash)
            {

                sbHash.Append(String.Format("{0:x2}", b));
            }
            return sbHash.ToString();
        }
        public string Radomchar()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var stringChars = new char[8];
            var random = new Random();

            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }
            var finalString = new String(stringChars);
            return finalString.ToString();
            //
        }
//-----------------------------------------------------------//
        public static void Insert(ETbDangNhap OTbDangNhap)
        {
            SqlParameter[] pr = new SqlParameter[5];
            pr[0] = new SqlParameter(@"TenDangNhap", OTbDangNhap.TenDangNhap);
            pr[1] = new SqlParameter(@"MatKhau", OTbDangNhap.MatKhau);
            pr[2] = new SqlParameter(@"Quyen", OTbDangNhap.Quyen);
            pr[3] = new SqlParameter(@"RadomPass", OTbDangNhap.RadomPass);
            pr[4] = new SqlParameter(@"KhoaTaiKhoan", OTbDangNhap.KhoaTaiKhoan);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDangNhap_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbDangNhap OTbDangNhap)
        {
            SqlParameter[] pr = new SqlParameter[6];
            pr[0] = new SqlParameter(@"MaDN", OTbDangNhap.MaDN);
            pr[1] = new SqlParameter(@"TenDangNhap", OTbDangNhap.TenDangNhap);
            pr[2] = new SqlParameter(@"MatKhau", OTbDangNhap.MatKhau);
            pr[3] = new SqlParameter(@"Quyen", OTbDangNhap.Quyen);
            pr[4] = new SqlParameter(@"RadomPass", OTbDangNhap.RadomPass);
            pr[5] = new SqlParameter(@"KhoaTaiKhoan", OTbDangNhap.KhoaTaiKhoan);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDangNhap_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaDN)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaDN",MaDN);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDangNhap_Delete", pr);
        }

        public static void DeleteByTenDN(string MaGV)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaGV", MaGV);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDangNhap_DeleteByTenDN", pr);
        }

        //
//-----------------------------------------------------------//
        public static ETbDangNhap SelectByID(int MaDN)
        {
            ETbDangNhap OTbDangNhap = new ETbDangNhap();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaDN",MaDN);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbDangNhap_SelectByID", pr);
            if (idr.Read())
              OTbDangNhap = GetOneTbDangNhap(idr);
            idr.Close();
            idr.Dispose();
            return OTbDangNhap;
        }
//-----------------------------------------------------------//
        private static ETbDangNhap GetOneTbDangNhap(IDataReader idr)
       {
            ETbDangNhap OTbDangNhap = new ETbDangNhap();
                if (idr["MaDN"] != DBNull.Value)
                    OTbDangNhap.MaDN = (int)idr["MaDN"];
                if (idr["TenDangNhap"] != DBNull.Value)
                    OTbDangNhap.TenDangNhap = (string)idr["TenDangNhap"];
                if (idr["MatKhau"] != DBNull.Value)
                    OTbDangNhap.MatKhau = (string)idr["MatKhau"];
                if (idr["Quyen"] != DBNull.Value)
                    OTbDangNhap.Quyen = (string)idr["Quyen"];
                if (idr["RadomPass"] != DBNull.Value)
                    OTbDangNhap.RadomPass = (string)idr["RadomPass"];
                if (idr["KhoaTaiKhoan"] != DBNull.Value)
                    OTbDangNhap.KhoaTaiKhoan = (int)idr["KhoaTaiKhoan"];
            return OTbDangNhap;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaDN)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaDN",MaDN);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDangNhap_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbDangNhap> ListAll()
        {
            List<ETbDangNhap> list = new List<ETbDangNhap>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbDangNhap_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbDangNhap(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbDangNhap> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbDangNhap> list = new List<ETbDangNhap>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbDangNhap_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbDangNhap(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}